//<>// //<>// //<>//

/* incoming osc message are forwarded to the oscEvent method. */

/* these fetch routines could be expanded to return an array with other info
 such as string info like "on" and... ??  */
String addr="";
boolean setWindowAddress=false;
boolean setWindowSize=false;
boolean showBloomAddress=false;
boolean makeToneTrigger=false;

float oscFloatFetch (OscMessage theOscMessage, int which) {
  float theValue=theOscMessage.get(which).floatValue();
  return theValue;
}

void keyPressed() {
  KeyAct=true;
  inputMsg="Key "+key+" = "+int(key);
  println(inputMsg);
  int action=-1;
  int keyIn=int(key);
  float keyFlo=0.0;
  
  // manually change the bloom address
  switch (keyIn) {
    case 92:
    envSetup();
    break;
  case 91: // '['
    action=4;
    keyIn=1;
    break;
  case 93: // ']'
    action=4;
    keyIn=2;
    break;
  default:
    break;
  }
  if (setWindowAddress) {
    if ((keyIn>47)&&(keyIn<53)) {
      setWindowSize=true;
    }
    if (keyIn==45) { // the "-" key
      monitorMode=!monitorMode;
      setWindowAddress=false;
    }
  }
  // set window size and positon
  if ((setWindowSize)&&(!android)) {
    if ((keyIn>47)&&(keyIn<53)) {  // '0' thru '4'
      int code = keyIn-48;
      int bloomI = 0;
      if (code>0) bloomI = int(bloom[0]);
      positionWindow(code, bloomI);
    }
    setWindowSize=false;
  }
  //tone test for Conductor only
  if (keyIn==61) {  // '=' key
    if (!android) {
      println("maketone " +makeTone);
      makeToneTrigger=!makeToneTrigger;
    }
  }
  if (keyIn==47) {
    showBloomAddress=!showBloomAddress;
    if (!showBloomAddress) {
      background(0);
    }
  }
  if (keyIn==8) { // 'backspace'
    action=0;
  }
  if (keyIn==10) {  // 'enter'
    action=3;
  }
  //! toggle bloom state; action 1
  if ((keyIn>96)&&(keyIn<113)) {
    keyIn-=97;
    if (setWindowAddress) {
      char reBloom = char(keyIn+65);
      bloom[0]=str(keyIn);
      bloom[1]=str(reBloom);
      makeToneTrigger=true;
      println("bloom0 now= "+bloom[1]);
      if (monitorMode) {
        positionWindow(5, int(bloom[0]));
      }
    } else {
      action=1; // 1 = flower switch
    }
  }
  //! set the active facet; action 2
  if ((keyIn>64)&&(keyIn<81)) {
    action=2; // 2 = facet switch
    keyIn-=65;
  }
  //! send to the echo handler
  if (action>0) {
    echoAction(action, keyIn, keyFlo);
  }

  setWindowAddress=false; //clear the state if the proper keys weren't entered
  if (keyIn==96) {
    setWindowAddress=true;
    println("reasign this window to a new listener:");
  }
  DisplayAct=true;
  KeyAct=false;
}//** end keyPressed() **

void echoAction(int action, int keyIn, float keyFlo) {
  int oscIndex=keyIn+1;
  boolean state=false;
  OscMessage echoControl;
  switch(action) {
  case 0:
    statMsg="clear controls";
    println (statMsg);
    //transition selector
    echoControl = new OscMessage("/MAIN/transition");
    echoControl.add(0.0);
    echoControl.add(1.0);
    oscP5x.send(echoControl, Conductor);
    //flowers
    echoControl = new OscMessage("/MAIN/FlowerState");
    for (int i : flowers) {
      flowers[i]=0;
      echoControl.add(0.0);
    } 
    oscP5x.send(echoControl, Conductor);
    //facets    
    echoControl = new OscMessage("/MAIN/FacetState");
    for (int i : facets) {
      facets[i]=0;
      echoControl.add(0.0);
    }   
    oscP5x.send(echoControl, Conductor);
    break;
  case 1:
    if (flowers[keyIn]==1) {
      state = false;
    } else {
      state = true;
    }
    statMsg = "Flower "+keyIn+ " " +state;
    println (statMsg);
    flowers[keyIn]=int(state);
    echoControl = new OscMessage("/MAIN/FlowerState/1/"+oscIndex);
    echoControl.add(flowers[keyIn]); 
    oscP5x.send(echoControl, Conductor);
    break;
  case 2:
    statMsg = "Facet "+facetName[keyIn]; 
    println (statMsg);
    echoControl = new OscMessage("/MAIN/FacetState");
    for (int i=0; i< facets.length; i++) {
      if (i==keyIn) {
        facets[i]=1;
        echoControl.add(1.0);
      } else {
        facets[i]=0;
        echoControl.add(0.0);
      }
    }
    knownFacet=keyIn;
    oscP5x.send(echoControl, Conductor);
    break; 
  case 3:
    statMsg="Launch "+facetName[knownFacet];
    println ("statMsg");
    echoControl = new OscMessage("/MAIN/go");
    echoControl.add(1.0);
    oscP5.send(echoControl);
    echoControl = new OscMessage("/MAIN/go");
    echoControl.add(0.0);
    oscP5.send(echoControl);
    break;
  case 4:
    transMode=keyIn;
    echoControl = new OscMessage("/MAIN/transition");
    if (transMode==1) {
      statMsg="Fade enabled";
      echoControl.add(1.0);
      echoControl.add(0.0);
    } else {
      statMsg=("Cut enabled");
      echoControl.add(0.0);
      echoControl.add(1.0);
    }
    println (statMsg);
    println (echoControl);
    oscP5x.send(echoControl, Conductor);
    break;
  case 5:
    RotZ+=keyFlo;
    FacetAct=true;
    break;
  default:
    break;
  }
  DisplayAct=true;
}

void oscEvent(OscMessage theOscMessage) {
  OscAct=true;
  addr = theOscMessage.addrPattern();
  inputMsg=addr;

  /* print the address pattern and the typetag of the received OscMessage 
   then route to the proper handler by sakura control type */

  print("### received an osc message: "+theOscMessage);
  println(" addrpattern: "+addr);
  println(" typetag: "+theOscMessage.typetag());

  //Transition
  if (addr.indexOf("transition")>-1) {
    transState(theOscMessage);
  }

  //Clear
  if (addr.indexOf("clear")>-1) {
    echoAction(0, 0,0.0);
  }

  // FlowerState
  if (addr.indexOf("FlowerState/1/")>-1) {
    flowerState(theOscMessage);
  }
  // FacetState  
  if (addr.indexOf("FacetState/1/")>-1) {
    facetState(theOscMessage);
  }

  //**** LISTEN TRIGGER controls for facets*************

  if ((flowers[15]==1)||(flowers[int(bloom[0])]==1)) {
    println("thru flower filter gate"); 
    // rot
    if (addr.indexOf("rot")>-1) {
      println("reading the rot encoder");
      sendRot(theOscMessage);
    }

    // xy1  
    if (addr.indexOf("xy1")>-1) {
      println("reading the XY slider");
      sendXY(theOscMessage);
    }
    // rotary1-4, slider1-4
    if (addr.indexOf("fader")>-1) {
      print("fading out...");
      sendFade(theOscMessage);
    }
    //disrupt  
    if (addr.indexOf("color")>-1) {
      sendDisrupt(theOscMessage);
    }
    //GO  
    if (addr.indexOf("go")>-1) {
      println("launch!");
      facetGo();
    }
  }
  DisplayAct=true;
  OscAct=false;
} // end oscEvent

void sendRot(OscMessage theOscMessage) {
  float rotValue=oscFloatFetch(theOscMessage, 0);
  println("rot value: "+rotValue);
  echoAction(5, 0, rotValue);
}
void sendXY(OscMessage theOscMessage) {
  float xValue=oscFloatFetch(theOscMessage, 0);
  float yValue=oscFloatFetch(theOscMessage, 1);
  println ("X: "+xValue+ " Y: " +yValue);
  oscX=xValue*width;
  mouseX=int(xValue*width);
  oscY=yValue*width;
  mouseY=int(xValue*height);
  //  now hook up the data
}

void sendDisrupt(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int colorValue = int(buffer[3]);    
  println ("Disrupt with hue: "+colorValue);
}

void sendFade(OscMessage theOscMessage) {
  float faderValue=oscFloatFetch(theOscMessage, 0);
  String buffer2[] = split(addr, "/");
  //s
  int thisFader = int(buffer2[3])-1;
  ranges[thisFader]=faderValue;
  println ("Fader "+thisFader+ " set at " +faderValue);
  //  flowers[thisFlower]=int(toggleValue);
}

void transState(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int thisFlower = (int(buffer[3])-1);
  echoAction(4, thisFlower, 0.0);
}

void flowerState(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int thisFlower = (int(buffer[4])-1);
  echoAction(1, thisFlower, 0.0);
}

void facetState(OscMessage theOscMessage) {
  String msg1="";
  float toggleValue=oscFloatFetch(theOscMessage, 0);
  String buffer[] = split(addr, "/");
  int thisFacet = (int(buffer[4])-1);
  if (toggleValue>0) {
    msg1="ON";
  } else {
    msg1="OFF";
  }
  println ("Facet "+thisFacet+ ", " +facetName[thisFacet]+" "+msg1);
  echoAction(2, thisFacet, 0.0);
}

void facetGo() {
  if (knownFacet != 16) {
    FacetAct=true;
    goFacet=knownFacet;
  } else { 
    FacetAct=false;
  }
  int launch=-1;
  for (int i =0; i<facets.length;i++) {
    if (facets[i]==1) {
      launch =i;
      println("this is the launch code: "+i);
      break;
    }
  }
  System.gc();  // *** does this do anything at all?
  switch (launch) {
  case 0:
    bloomFacet=true;
    break;
  case 2:
    break;
  case 14:
    setupFireWorksFF();
    fireWorksFacet=true;
    println("should launch fireworks");
    break;  
  case 15:
    flowerFacet=true;
  default:
    break;
  }
}

//end oscTransponder
